home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3446 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.0 KB  |  141 lines

  1. Path: colossus.holonet.net!tglbbs!charles.herold
  2. From: charles.herold@tglbbs.com (Charles Herold)
  3. Newsgroups: comp.lang.c
  4. Subject: array of struct pointer switching difficulties
  5. Date: Fri, 26 Jan 1996 18:48:17 GMT
  6. Message-ID: <96012819461829517@tglbbs.com>
  7. Organization: TGL BBS 3 Nodes of Fun 212-974-3925, 212-765-2524, 212-541-5596
  8. Distribution: world
  9.  
  10. I posted a question regarding reversing an array of pointers.  Two
  11. people posted similar answers, but neither works, so perhaps there is a
  12. more fundamental problem with my code.  Therefore I have put together a
  13. full little program that will get the files of a directory, reverse the
  14. order they're in, and print them out, so people can see what I've done,
  15. and hopefully tell me what I'm doing wrong.  In SortFiles, a version of
  16. which I posted last time, I have both the reverse sort I use and the one
  17. suggested to me.  I apologize for posting such a long piece of code, but
  18. this seems to be the minimum necessary to show you what the problem is.
  19.  
  20. Thanks to anyone willing to go through all this.  This will compile in
  21. QuickC, and I believe if you were to replace _dos_findfirst/next with
  22. the appropriate functions that it would compile in anything else.
  23. /***************** CUT HERE *******************/
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <dos.h>
  27. #include <string.h>
  28.  
  29. struct dir_window {
  30.     int numberfiles;   /* number files found */
  31.     struct find_t **files;
  32. };
  33. struct find_t *files;
  34. int reverse_sort = 1;   /* whether to reverse the order */
  35. struct dir_window window;
  36. /* next two declarations can be ignored, although the first will get
  37.  * a link error warning.  They are just here so I don't have
  38.  * to rewrite a couple of things
  39.  */
  40. int (*sorts[])();
  41. int sort_proc;
  42. /*resizeArray() resizes an array of struct find_t.  Used with
  43. Directory()*/
  44. struct find_t *resizeArray( struct find_t *files, int     size)
  45. {
  46.     if( (files = realloc( files, sizeof( struct find_t) * size)) ==
  47.          NULL) {
  48.          perror( "" );
  49.          fprintf( stderr, "Unable to allocate memory. size = %d\n",
  50.               size);
  51.          exit(1);
  52.     }
  53.     return files;
  54. }
  55. /*********************************************************
  56.  * Directory() puts all specified files in find_t structure in files
  57. array  *  returns:    number of files found  */
  58. #define ALLOC_SIZE 10
  59. int Directory( char *filespec, unsigned attribute, struct find_t
  60. **filesPtr )
  61. {
  62.     int number_found = 0,
  63.     allocated = 0;
  64.     struct find_t tmpBuff;
  65.  
  66.     if( _dos_findfirst( filespec, attribute, &tmpBuff ) ) {
  67.          /* it wasn't suitable*/
  68.          *filesPtr = NULL; /* set it NULL just for safety */
  69.          return 0;
  70.     }
  71.     do {
  72.          if( number_found >= allocated)
  73.          (*filesPtr) = resizeArray( *filesPtr, (allocated +=
  74.               ALLOC_SIZE));
  75.          (*filesPtr)[number_found++] = tmpBuff; }
  76.     while( !_dos_findnext(&tmpBuff) );
  77.     resizeArray( *filesPtr, number_found); /* get rid of any extras */
  78.     return number_found;
  79. }
  80. /* SortFiles() generally sorts files with qsort, which uses one of
  81.  *             five different compare functions depending on user
  82.  *             input.  That's been cut out, leaving the part
  83.  *             that will reverse a sort.  I want to reverse the
  84.  *             sort by reversing what the pointers point to, but
  85.  *             seem unable to make this work.  */
  86.  
  87. void SortFiles( struct dir_window *dwindow, int (*compare)(const
  88.     void *elem1, const void *elem2))
  89. {
  90.     int i, h;
  91.     struct find_t temp, **files = dwindow->files;
  92.  
  93.     /* There's a qsort here I've removed as extraneous */
  94.     if( reverse_sort ) {    /* make it all backwards */
  95. #if defined SHOULD_WORK   /* told this should work, but doesn't */
  96.          struct find_t *temp;
  97.         for (i=0; i < dwindow->numberfiles / 2; i++) {
  98.               temp = files[i];
  99.               files[i] = files[dwindow->numberfiles - i - 1];
  100.              files[dwindow->numberfiles - i - 1] = temp;
  101.          }
  102. #else
  103.         for( i = 0; i < dwindow->numberfiles / 2; i++ ) {
  104.               /* now reverse the entire array */
  105.               temp = (*files)[i];
  106.              (*files)[i] = (*files)[dwindow->numberfiles - i - 1];
  107.               (*files)[dwindow->numberfiles - i - 1] = temp;
  108.         }
  109. #endif
  110.     }
  111. }
  112. /* GetDirectory() loads a list of directories into files and returns
  113.  the number of listings*/
  114. void
  115. PrintDir( struct dir_window window )
  116. {
  117.     int i;
  118.     struct find_t **files = window.files;
  119.  
  120.     for( i = 0; i < window.numberfiles; i++ )
  121.          puts( (*files)[i].name );
  122. }
  123. int GetDirectory( struct dir_window *dwindow, struct find_t **files )
  124. {
  125.     dwindow->numberfiles = Directory( "*.*", 0xffff, files );
  126.     if( dwindow->numberfiles < 1 )
  127.          return 0;
  128.     dwindow->files = files;
  129.     SortFiles( dwindow, sorts[sort_proc] );
  130.     PrintDir( window );
  131.     return dwindow->numberfiles;
  132. }
  133. main( int argc, char *argv[] )
  134. {
  135.     if( !(window.numberfiles = GetDirectory( &window, &files) ) ) {
  136.          fprintf( stderr, "no files found" );
  137.          return 1;
  138.     }
  139. }
  140. /***************** CUT HERE **********************/
  141.